package com.example.tse.rbhttpservice.service.member;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberOrderConfirmReceiveRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberOrderConfirmReceiveResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberOrderGetRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberOrderGetResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberOrderGetStatRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberOrderGetStatResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberOrderGetUnpaidList;
import com.example.tse.rbhttpservice.service.member.model.MemberOrderGetUnpaidListRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberOrderGetUnpaidListResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;





/**
 *
 * @author wliu
 *    edi  tse
 */
public class MemberOrderServiceImpl implements MemberOrderService {
	private Context context;
	private static Gson gson = new Gson();


	public MemberOrderServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 已收货的订单，用户可以主动点击确认收货
	 * @param request
	 * @param listener
	 * @return MemberOrderConfirmReceiveResponse
	 */

	    public void confirmReceive(MemberOrderConfirmReceiveRequest request, final RemoteServiceListenerT< MemberOrderConfirmReceiveResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("orderId",request.getOrderId());

        final String url = "member/order/confirmReceive";


            new HttpService().post(context, url, params, MemberOrderConfirmReceiveResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MemberOrderConfirmReceiveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MemberOrderConfirmReceiveResponse() : (MemberOrderConfirmReceiveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MemberOrderConfirmReceiveResponse() : (MemberOrderConfirmReceiveResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 获取我的订单详情，展示订单详细信息用，或再次支付用
	 * @param request
	 * @param listener
	 * @return MemberOrderGetResponse
	 */

	    public void get(MemberOrderGetRequest request, final RemoteServiceListenerT< MemberOrderGetResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("orderId",request.getOrderId());

        final String url = "member/order/get";


            new HttpService().get(context, url, params, MemberOrderGetResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MemberOrderGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MemberOrderGetResponse() : (MemberOrderGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MemberOrderGetResponse() : (MemberOrderGetResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 获取我的待支付订单列表
	 * @param request
	 * @param listener
	 * @return MemberOrderGetUnpaidListResponse
	 */

	    public void getUnpaidList(MemberOrderGetUnpaidListRequest request, final RemoteServiceListenerT< MemberOrderGetUnpaidListResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("status",request.getStatus());

        final String url = "member/order/getUnpaidList";



        /**
        order
        getUnpaidList
        GetUnpaidList
        获取我的待支付订单列表
        GET
        MemberOrder
        MemberOrderGetUnpaidList
        member/order/getUnpaidList
        array
        */

           final Type type = new TypeToken<List<MemberOrderGetUnpaidList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MemberOrderGetUnpaidListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MemberOrderGetUnpaidList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MemberOrderGetUnpaidListResponse responce = new MemberOrderGetUnpaidListResponse();
                 responce.setGetUnpaidList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MemberOrderGetUnpaidListResponse());
                 } else {
                     List<MemberOrderGetUnpaidList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberOrderGetUnpaidListResponse responce = new MemberOrderGetUnpaidListResponse();
                     responce.setGetUnpaidList(items);
                     listener.okData(rudderResponse == null ? new MemberOrderGetUnpaidListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MemberOrderGetUnpaidListResponse(), fromWhere);
                 } else {
                     List<MemberOrderGetUnpaidList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberOrderGetUnpaidListResponse responce = new MemberOrderGetUnpaidListResponse();
                     responce.setGetUnpaidList(items);
                     listener.okData(rudderResponse == null ? new MemberOrderGetUnpaidListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }

             @Override
             public void onMessage(String message) {
                 super.onMessage(message);
                 listener.onMessage(message);
             }


         });
    }






	/**
	 * 获取用户的待发货、待收货的订单数量，显示在个人中心的待发货、待收货图标的角标上，
	 * @param request
	 * @param listener
	 * @return MemberOrderGetStatResponse
	 */

	    public void getStat(MemberOrderGetStatRequest request, final RemoteServiceListenerT< MemberOrderGetStatResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "member/order/getStat";


            new HttpService().get(context, url, params, MemberOrderGetStatResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MemberOrderGetStatResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MemberOrderGetStatResponse() : (MemberOrderGetStatResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MemberOrderGetStatResponse() : (MemberOrderGetStatResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




}